// GStatic.cpp : implementation file
//

#include "stdafx.h"
#include "GStatic.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CGStatic

CGStatic::CGStatic()
{
	m_bHasBorder = FALSE;
	m_borderColor = 0;
	m_textColor = 0;
	m_pBorderColor = &m_borderColor;
	m_pTextColor = &m_textColor;
}

CGStatic::~CGStatic()
{
}


BEGIN_MESSAGE_MAP(CGStatic, CStatic)
	//{{AFX_MSG_MAP(CGStatic)
	ON_WM_NCPAINT()
	ON_WM_CTLCOLOR_REFLECT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGStatic message handlers

void CGStatic::drawTransparent(BOOL bRefresh)
{
m_brush.DeleteObject();
}
void CGStatic::drawTransparent(CDC *pDC,BOOL bRefresh /* = FALSE */)
{

	InitDataBase();
	CRect rectScr;
	RECT rect;
	GetClientRect(&rect);
	GetWindowRect(&rectScr);
	int cx = (rectScr.Width() - rect.right)/2;
	int cy = (rectScr.Height() - rect.bottom)/2;
	GetParent()->ScreenToClient(&rectScr);
	CDC mDC;
	mDC.CreateCompatibleDC(pDC);
	m_bkBMP.CreateCompatibleBitmap(pDC,rect.right,rect.bottom);
	mDC.SelectObject(m_bkBMP);
	mDC.BitBlt(0,0,rect.right,rect.bottom, pDC, rectScr.left+cx, rectScr.top+cy, SRCCOPY);
	mDC.DeleteDC();
	m_brush.CreatePatternBrush(&m_bkBMP);

}
void CGStatic::SetBkColor(COLORREF bkColor,BOOL bRefresh /* = FALSE */)
{
	InitDataBase();
	m_brush.CreateSolidBrush(bkColor);
	if (bRefresh)
		Invalidate();
}

void CGStatic::SetBkBMP(CBitmap *bkBMP,BOOL bRefresh /* = FALSE */)
{
	Invalidate();
	m_brush.CreatePatternBrush(bkBMP);
	if (bRefresh)
		Invalidate();
}

void CGStatic::SetBorder(BOOL bHasBoder /* = TRUE */,COLORREF BoderColor /* = 0 */,BOOL bRefresh /* = FALSE */)
{
	if (bHasBoder)
	{
		m_borderColor = BoderColor;
		m_pBorderColor = &m_borderColor;

	}
	if (bRefresh)
		RedrawWindow(NULL,NULL,RDW_INVALIDATE|RDW_FRAME);
}

void CGStatic::SetBorder(BOOL bHasBoder /* = TRUE */,COLORREF *BoderColor /* = 0 */,BOOL bRefresh /* = FALSE */)
{
	if (bHasBoder)
	{
		m_pBorderColor = BoderColor;
		m_bHasBorder = bHasBoder;//xing:
	}
	if (bRefresh)
		RedrawWindow(NULL,NULL,RDW_INVALIDATE|RDW_FRAME);
}

void CGStatic::Redraw(BOOL bRedraw)
{
	if (bRedraw)
		RedrawWindow(NULL,NULL,RDW_INVALIDATE|RDW_FRAME);
}

void CGStatic::InitDataBase()
{
	if(m_brush.m_hObject)
		m_brush.DeleteObject();
	if (m_bkBMP.m_hObject)
		m_bkBMP.DeleteObject();
}

void CGStatic::OnNcPaint()
{
	// TODO: Add your message handler code here
	CWindowDC dc(this); 

	if (m_bHasBorder)
	{
	CRect rc; 
	GetWindowRect(&rc); 
	rc.right-=rc.left; //width 
	rc.bottom-=rc.top; //height 
	rc.top=0; 
	rc.left=0;
	//CBrush brush;
	//brush.CreateSolidBrush(*m_pBorderColor);
	CBrush brush(RGB(255, 0, 0));
	dc.FrameRect(&rc,&brush);
	}
	// Do not call CDialog::OnNcPaint() for painting messages
}

void CGStatic::SetTextColor(COLORREF textColor)
{
	m_textColor = textColor;
	m_pTextColor = &m_textColor;
}

void CGStatic::SetTextColor(COLORREF *textColor)
{
	m_pTextColor = textColor;
}

HBRUSH CGStatic::CtlColor(CDC* pDC, UINT nCtlColor) 
{
 // TODO: Change any attributes of the DC here
	pDC->SetTextColor(*m_pTextColor);
	pDC->SetBkMode(TRANSPARENT);
 	if(m_brush.m_hObject)
		return m_brush;

	SetBk(pDC);
	return (HBRUSH)GetStockObject(NULL_BRUSH);

}


void CGStatic::SetBk(CDC *pDC)
{
	InitDataBase();
	CRect rectScr;
	RECT rect;
	GetClientRect(&rect);
	GetWindowRect(&rectScr);
	int cx = (rectScr.Width() - rect.right)/2;
	int cy = (rectScr.Height() - rect.bottom)/2;
	GetParent()->ScreenToClient(&rectScr);
	CDC mDC;
	mDC.CreateCompatibleDC(pDC);
	m_bkBMP.CreateCompatibleBitmap(pDC,rect.right,rect.bottom);
	mDC.SelectObject(m_bkBMP);
	mDC.BitBlt(0,0,rect.right,rect.bottom, pDC, rectScr.left+cx, rectScr.top+cy, SRCCOPY);
	mDC.DeleteDC();
	m_brush.CreatePatternBrush(&m_bkBMP);
}

/// basic check by fan05